package com.airycode.distribution.distribution1.controller;

import com.airycode.distribution.distribution1.domain.UserEntity;
import com.airycode.distribution.distribution1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by zkr-fanxirui on 2017/8/15.
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;





    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpServletResponse response){
        String requestPage = "";
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> dataMap = new HashMap<String,Object>();
        String userName = request.getParameter("userName");
        String password = request.getParameter("userPassword");
        UserEntity userEntity = userService.getUserByNameAndPassword(userName,password);
        if (userEntity != null) {
            requestPage="main";
        }
        dataMap.put("userName",userName);
        resultMap.put("dataMap",dataMap);
        request.setAttribute("resultMap",resultMap);
        return requestPage;
    }

    //安全退出

    @RequestMapping(value = "/loginout",method = RequestMethod.GET)
    public String loginout(HttpServletRequest request, HttpServletResponse response){
        String requestPage = "";
        //清除session然后浏览器的缓存
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        requestPage="login";
        return requestPage;
    }




    @RequestMapping(value = "/registerToPage",method = RequestMethod.GET)
    public String registerToPage(HttpServletRequest request, HttpServletResponse response){
        String requestPage = "";
        requestPage = "register";
        return requestPage;
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(HttpServletRequest request, HttpServletResponse response){
        String requestPage = "";
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String userPassProblem = request.getParameter("userPassProblem");
        String userPassAnswer = request.getParameter("userPassAnswer");
        String userPhone = request.getParameter("userPhone");
        String userMail = request.getParameter("userMail");
        UserEntity userEntity = new UserEntity();
        userEntity.setId(UUID.randomUUID().toString());
        userEntity.setUserName(userName);
        userEntity.setUserPassword(userPassword);
        userEntity.setUserMail(userMail);
        userEntity.setUserPhone(userPhone);
        userEntity.setUserPassProblem(userPassProblem);
        userEntity.setUserPassAnswer(userPassAnswer);
        boolean flag = userService.addUser(userEntity);
        if (flag) {
            requestPage="redirect:/index";
        } else {
            //requestPage="";
        }
        return requestPage;
    }
    @RequestMapping(value = "/forget",method = RequestMethod.GET)
    public String forget(){
        String resultPage = "";
        resultPage = "forget";
        return resultPage;
    }













}
